<template>
  <div class="bottom-menu ignore">
    <check-button
      class="select-all"
      v-model="isSelectAll"
      @selectChange="allSelectChange"
    ></check-button>
    <span>全选</span>
    <span class="total-price">合计:￥{{ totalPrice }}</span>
    <span class="buy-product">结算({{ sumCount }})</span>
  </div>
</template>

<script>
import checkButton from "components/content/checkButton/checkButton";
import { mapGetters } from "vuex";

export default {
  name: "cartBottemBar",
  components: {
    checkButton,
  },
  computed: {
    ...mapGetters({
      cartLength: "cartLength",
      cartList: "cartList",
    }),
    totalPrice() {
      return this.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preV, item) => {
          return preV + item.count * item.price;
        }, 0)
        .toFixed(2);
    },
    isSelectAll() {
      if (this.cartLength === 0) return false;
      return this.cartList.find((item) => item.checked === false) === undefined;
      // this.cartList.find((item) => !item.checked);
    },
    sumCount() {
      return this.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preV, item) => {
          return preV + item.count;
        }, 0);
    },
  },
  methods: {
    allSelectChange() {
      //判断购物车商品是否有未被选择的
      let deSelect = this.cartList.find((item) => !item.checked);
      //如果有，则选中
      if (deSelect) {
        this.cartList.forEach((element) => {
          element.checked = true;
        });
      } else {
        this.cartList.forEach((element) => {
          element.checked = false;
        });
      }
    },
  },
};
</script>

<style scoped>
.bottom-menu {
  width: 100%;
  height: 44px;
  background-color: #eee;
  position: fixed;
  bottom: 49px;
  left: 0;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, 0.2);
  font-size: 14px;
  color: #888;
  line-height: 44px;
  padding-left: 35px;
  box-sizing: border-box;
}
.bottom-menu .select-all {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 13px;
}

.bottom-menu .total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.bottom-menu .buy-product {
  background-color: orangered;
  color: #fff;
  width: 100px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  float: right;
}
</style>